package cn.lili.modules.grab.dingdong;

import cn.lili.common.exception.ServiceException;
import cn.lili.common.utils.ResultUtil;
import cn.lili.common.vo.ResultMessage;
import cn.lili.exchange.AmqpExchangeProperties;
import cn.lili.modules.goods.client.GoodsSkuClient;
import cn.lili.modules.goods.entity.dos.Goods;
import cn.lili.modules.goods.entity.dos.GoodsSku;
import cn.lili.modules.goods.service.GoodsService;
import cn.lili.modules.grab.GrabOrderParam;
import cn.lili.modules.grab.dingdong.base.BaseResult;
import cn.lili.modules.grab.dingdong.config.DingdongConfig;
import cn.lili.modules.grab.dingdong.reps.order.CreateOrderRespose;
import cn.lili.modules.grab.dingdong.reps.order.FreightCalcRespose;
import cn.lili.modules.grab.dingdong.reps.order.GetOrderInfoRespose;
import cn.lili.modules.grab.dingdong.res.order.CreateOrderRequest;
import cn.lili.modules.grab.dingdong.res.order.FreightCalcRequest;
import cn.lili.modules.grab.dingdong.service.DingdongOrderService;
import cn.lili.modules.grab.enums.SourceEnums;
import cn.lili.modules.grab.enums.StatusEnums;
import cn.lili.modules.grab.factory.order.GrabOrderService;
import cn.lili.modules.order.order.client.OrderItemClient;
import cn.lili.modules.order.order.entity.dos.OrderItem;
import cn.lili.modules.system.client.RegionQddClient;
import cn.lili.modules.system.entity.dos.RegionQdd;
import cn.lili.modules.system.entity.dos.RequestLog;
import cn.lili.modules.system.enums.RequestLogTypeEnum;
import cn.lili.routing.ThirdCallLogKey;
import cn.lili.util.AmqpMessage;
import cn.lili.util.AmqpSender;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

@Component
@Slf4j
public class GrabDingdongOrderServiceImpl implements GrabOrderService {

    @Resource
    private DingdongOrderService dingdongOrderService;
    @Resource
    private GoodsSkuClient goodsSkuClient;
    @Resource
    private GoodsService goodsService;
    @Resource
    private OrderItemClient orderItemClient;
    @Resource
    private RegionQddClient regionQddClient;

    @Override
    public SourceEnums step() {
        return SourceEnums.DINGDONG_ORDER;
    }

    /**
     * 运费计算
     *
     * @param grabParam 参数
     */
    @Override
    public ResultMessage<Object> freightCalc(GrabOrderParam grabParam) {
        log.info("企叮当--运费计算入参----{}", JSONObject.toJSONString(grabParam));
        FreightCalcRespose freightCalcRespose = dingdongOrderService.freightCalc(grabParam.getFreightCalcRequest());
        //其它业务逻辑
        return ResultUtil.data(freightCalcRespose);
    }

    /**
     * 创建订单
     *
     * @param grabParam 参数
     */
    @Override
    public ResultMessage<Object> createOrder(GrabOrderParam grabParam) {
        log.info("企叮当--创建订单入参----{}", JSONObject.toJSONString(grabParam));


        //1.查询配送方式  默认配送方式 1
        String yf_mode = "1";
        String[] addressId = grabParam.getOrder().getConsigneeAddressIdPath().split(",");
        String provinceId = addressId[0];
        String cityId = addressId[1];
        String areaId = addressId[2];
        RegionQdd province = regionQddClient.getRegionQddById(provinceId);
        RegionQdd city = regionQddClient.getRegionQddById(cityId);
        RegionQdd area = regionQddClient.getRegionQddById(areaId);

        OrderItem orderItem = grabParam.getOrderItem();
        GoodsSku goodsSku = goodsSkuClient.getGoodsSkuById(orderItem.getSkuId());
        Goods goods = goodsService.getById(goodsSku.getGoodsId());
        //2.计算运费金额
        FreightCalcRequest freightCalcRequest = new FreightCalcRequest();
        freightCalcRequest.setProvince(province != null ? province.getRegionId() : null);
        freightCalcRequest.setCity(city != null ? city.getRegionId() : null);
        freightCalcRequest.setArea(area != null ? area.getRegionId() : null);
        freightCalcRequest.setGoods_id(goods.getThirdPartyPlatformId());
        freightCalcRequest.setNum(String.valueOf(orderItem.getNum()));
        freightCalcRequest.setSpec_id(goodsSku.getThirdPartyPlatformId());
        freightCalcRequest.setYf_mode(yf_mode);
        FreightCalcRespose freightCalcRespose = dingdongOrderService.freightCalc(freightCalcRequest);

        //3.构建商品信息
        CreateOrderRequest.Goods orderGoods = new CreateOrderRequest.Goods();
        orderGoods.setGoods_id(goods.getThirdPartyPlatformId());
        orderGoods.setSpec_id(goodsSku.getThirdPartyPlatformId());
        orderGoods.setNum(String.valueOf(orderItem.getNum()));
        orderGoods.setYf_mode(yf_mode);
        orderGoods.setShip_price(freightCalcRespose.getShip_price());
        orderGoods.setTotal_ship_price(freightCalcRespose.getTotal_ship_price());
        orderGoods.setBasic_ship_price(freightCalcRespose.getBasic_ship_price());
        orderGoods.setGoods_price(String.valueOf(goodsSku.getCost() * 100));
        List<CreateOrderRequest.Goods> goodsList = new ArrayList<>();
        goodsList.add(orderGoods);

        //构建下单
        CreateOrderRequest createOrderRequest = new CreateOrderRequest();
        createOrderRequest.setPhone(grabParam.getOrder().getConsigneeMobile());
        createOrderRequest.setConsignee(grabParam.getOrder().getConsigneeDetail());
        createOrderRequest.setProvince(province != null ? province.getRegionId() : null);
        createOrderRequest.setCity(city != null ? city.getRegionId() : null);
        createOrderRequest.setArea(area != null ? area.getRegionId() : null);
        createOrderRequest.setBuyer_name(grabParam.getOrder().getConsigneeName());
        createOrderRequest.setTrade_no(orderItem.getSn());
        createOrderRequest.setGoods(goodsList);
        //下单
        CreateOrderRespose createOrderRespose = dingdongOrderService.createOrder(createOrderRequest);
        //更新子订单
        orderItem.setOrderJsonVal(JSONObject.toJSONString(createOrderRespose));
        orderItem.setOrderItemIdVal(createOrderRespose.getOrder_sn());
        orderItemClient.updateById(orderItem);
        return ResultUtil.data(StatusEnums.STATUS_200.getDes());
    }

    /**
     * 订单详情
     *
     * @param grabParam 参数
     */
    @Override
    public ResultMessage<Object> getOrderInfo(GrabOrderParam grabParam) {
        log.info("企叮当--订单详情入参----{}", JSONObject.toJSONString(grabParam));
        List<GetOrderInfoRespose> getOrderInfoResposeList = dingdongOrderService.getOrderInfo(grabParam.getGetOrderInfoRequest());
        //其它业务逻辑
        return ResultUtil.data(getOrderInfoResposeList);
    }
}
